/**
 * @author xiexiaojin
 * @date 2025-02-18 15:11
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TuoMin {
    public static void main(String[] args) {
//        String text1 = "张三丰的身份证号是320106199901011234,手机号是18412345678,银行卡号是6222021234567890123";
        String text2 = "YXY110===cst_nm:张三丰，生码：CCB9916240105897384114540";
//        String text1="这就是一段文字，没有什么需要脱敏的";
        String text1="TXCODE=YXY950&BRANCHID=510000000&SERVLET_NAME=B2CMainPlat_00&CCB_IBSVersion=V6&PT_STYLE=10&AMOUNT=0.01&SHL_ID=10001&ORDERID=YXY95020250219092604&QRCODE=CCB9916240105897384114540&TERMNO=320f22474a99d5c9";
        // 脱敏处理并打印结果
        String maskedText1 = maskSensitiveInfo(text1);
        System.out.println("原来的文本："+text1);
        System.out.println("脱敏后的文本: " + maskedText1);

        // 脱敏处理并打印结果
        String maskedText2 = maskSensitiveInfo(text2);
        System.out.println("原来的文本："+text2);
        System.out.println("脱敏后的文本: " + maskedText2);
    }

    public static String maskSensitiveInfo(String text) {
        // 身份证号码匹配模式 (18位)
        String idCardRegex = "[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]";
        Pattern idCardPattern = Pattern.compile(idCardRegex);
        Matcher idCardMatcher = idCardPattern.matcher(text);
        while (idCardMatcher.find()) {
            String idCard = idCardMatcher.group();
            StringBuilder stars = new StringBuilder();
            for (int i = 0; i < idCard.length() - 8; i++) {
                stars.append("*");
            }
            String maskedIdCard = idCard.substring(0, 3) + stars + idCard.substring(idCard.length() - 4);
            text = text.replace(idCard, maskedIdCard);
        }

        // 手机号码匹配模式
        String phoneRegex = "1[3-9]\\d{9}";
        //String phoneRegex="^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
        Pattern phonePattern = Pattern.compile(phoneRegex);
        Matcher phoneMatcher = phonePattern.matcher(text);
        while (phoneMatcher.find()) {
            String phone = phoneMatcher.group();
            String maskedPhone = phone.substring(0, 3) + "****" + phone.substring(phone.length() - 4);
            text = text.replace(phone, maskedPhone);
        }

        // 银行卡号匹配模式 (16-19位数字)
        String bankCardRegex = "\\d{16,19}";
        Pattern bankCardPattern = Pattern.compile(bankCardRegex);
        Matcher bankCardMatcher = bankCardPattern.matcher(text);
        while (bankCardMatcher.find()) {
            String bankCard = bankCardMatcher.group();
            StringBuilder stars = new StringBuilder();
            for (int i = 0; i < bankCard.length() - 4; i++) {
                stars.append("*");
            }
            String maskedBankCard = stars.toString() + bankCard.substring(bankCard.length() - 4);
            text = text.replace(bankCard, maskedBankCard);
        }

        // 中文姓名匹配模式
        Pattern namePattern = Pattern.compile("[\u4e00-\u9fa5]{2,4}");
        Matcher nameMatcher = namePattern.matcher(text);
        StringBuffer sb = new StringBuffer();

        while (nameMatcher.find()) {
            String name = nameMatcher.group();
            String maskedName;
            if (name.length() == 2) {
                maskedName = name.charAt(0) + "*";
            } else {
                StringBuilder middleStars = new StringBuilder();
                for (int i = 0; i < name.length() - 2; i++) {
                    middleStars.append("*");
                }
                maskedName = name.charAt(0) + middleStars.toString() + name.charAt(name.length()-1);
            }
            nameMatcher.appendReplacement(sb, maskedName);
        }
        nameMatcher.appendTail(sb);

        return sb.toString();
    }
}
